package cc.yugu.yhdw.webserver.service.basic;


import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.AreaEntity;
import cc.yugu.yhdw.common.domain.basic.AddressBook;
import cc.yugu.yhdw.common.domain.basic.Passport;
import cc.yugu.yhdw.common.entity.AreaCache;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.common.service.AbstractService;
import cc.yugu.yhdw.webserver.persistence.basic.AddressBookMapper;
import cc.yugu.yhdw.webserver.persistence.basic.PassportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import java.beans.Transient;

import java.util.List;
/**
 * 地址簿
 *
 * @Date:Created in 2018/7/2 11:17
 * @Author:
 * @Modified By: zjn 2018/7/2 11:17
 */
@Service
public class AddressBookService extends AbstractService {
    @Autowired
    AddressBookMapper addressBookMapper;
    @Autowired
    AreaCache areaCache;
    @Autowired
    PassportMapper passportMapper;
    /**
    * 查询，分页
    *
    * @Param: [addressBook]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/2  16:00
    */
    public Page findPage(AddressBook addressBook) {
        // 获得分页的一些基本页面信息
        Page page = addressBook.buildPage();
        // 根据客户主键查询表bas_address_book，返回行数，，并设置总记录数到page对象
        page.setTotalItems(addressBookMapper.findPageCount(addressBook));
        // 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置, 序号从0开始, 并设为开始索引
        addressBook.setBeginIndex(page.getOffset());
        // 分页查询，根据客户id查询表bas_address_book 中指定的行数的数据 ，保存到集合中
        List<AddressBook> list = addressBookMapper.findPageResult(addressBook);
        // 根据（客户表）bas_passport 表中的主键id查询数据
        Passport passport = passportMapper.find(addressBook.getPassportId());
        for (AddressBook e : list) {
            if (passport.getAddressBookId() != null && passport.getAddressBookId().equals(e.getId())) {
                e.setDefaultAddress(ConstEnum.Flag.TRUE.getValue());    //1  为默认地址
            } else {
                e.setDefaultAddress(ConstEnum.Flag.FALSE.getValue());
            }
        }
        /*返回查询的地址集合*/
        page.setResult(setAreaProperties(areaCache, list));
        return page;
    }
    /**
    * 查询，返回地址
    *
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.AreaEntity
    * @Author By: zjn 2018/7/2  16:03
    */
    public AreaEntity find(Integer id) {
        // 根据id查询表bas_address_book
        AddressBook addressBook = addressBookMapper.find(id);
        // 根据客户id查询表 bas_passport 客户信息
        Passport passport = passportMapper.find(addressBook.getPassportId());
        if (passport.getAddressBookId() != null && passport.getAddressBookId().equals(addressBook.getId())) {
            addressBook.setDefaultAddress(1);
        } else {
            addressBook.setDefaultAddress(ConstEnum.Flag.FALSE.getValue());
        }
        // 返回查询的地址名称
        return setAreaProperties(areaCache, addressBook);
    }
    /**
    * 插入数据（@Transactional(rollbackFor = Throwable.class)：遇到异常Throwable（及其子类） 事务会发生回滚）
    *
    * @Param: [addressBook]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/2  16:05
    */
    @Transactional(rollbackFor = Throwable.class)
    public ExtResult insert(AddressBook addressBook) {
        try {
            // 根据客户id查询客户信息
            Passport passport = passportMapper.find(addressBook.getPassportId());
            // 将地址信息插入到地址簿
            addressBookMapper.insert(addressBook);
            if (addressBook.getDefaultAddress() == 1 || passport.getAddressBookId() == null) {
                passportMapper.updateAddressBookId(addressBook.getPassportId(), addressBook.getId());
            }
            // 当你的方法中抛出下面的异常时，它会将事务回滚，数据库中的数据将不会改变，也就是回到进入此方法前的状态。
        } catch (Exception e) {
            return ExtResult.failResult("操作失败");
        }

        return ExtResult.successResult();
    }
    /**
    * 更新地址
    *
    * @Param: [addressBook]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/2  16:23
    */
    @Transactional(rollbackFor = Throwable.class)
    public ExtResult update(AddressBook addressBook) {
        try {
            addressBookMapper.update(addressBook);
            if (addressBook.getDefaultAddress() == 1) {
                passportMapper.updateAddressBookId(addressBook.getPassportId(), addressBook.getId());
            }
        } catch (Exception e) {
            return ExtResult.failResult("操作失败");
        }
        return ExtResult.successResult();
    }
    /**
    * 根据id删除数据
    *
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/2  16:25
    */
    public ExtResult delete(Integer id) {
        try {
            AddressBook addressBook = addressBookMapper.find(id);
            Passport passport = passportMapper.find(addressBook.getPassportId());

            if (passport.getAddressBookId() != null && passport.getAddressBookId() == addressBook.getId()) {
                return ExtResult.failResult("不可以删除默认地址");
            }
            addressBookMapper.delete(id);
        } catch (Exception e) {
            return ExtResult.failResult("操作失败");
        }

        return ExtResult.successResult();
    }
}
